// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        var self = this;
        this.timer = 5;
        this.mainPageView = cc.find("Canvas/PageView");
        this.storeSmallPageView = cc.find("Canvas/PageView/view/content/store/SmallPageView");
        this.meSmallPageView = cc.find("Canvas/PageView/view/content/me/smallPageView");
        cc.loader.loadRes("MainAtlas/puzzle_1", cc.SpriteAtlas, function (err, atlas) {
            self.puzzleAtlas1 = atlas;
        });
        this.storeButton = cc.find("Canvas/Bottom/storeButton").getComponent(cc.Button);

        this.storeButton.node.on('click', function (event) {
            self.mainPageView.getComponent(cc.PageView).scrollToPage(0);
        });
        this.giftButton = cc.find("Canvas/Bottom/giftButton").getComponent(cc.Button);

        this.giftButton.node.on('click', function (event) {
            self.mainPageView.getComponent(cc.PageView).scrollToPage(1);
        });
        this.shelfButton = cc.find("Canvas/Bottom/shelfButton").getComponent(cc.Button);

        this.shelfButton.node.on('click', function () {
            self.mainPageView.getComponent(cc.PageView).scrollToPage(2);
        });
        this.mefButton = cc.find("Canvas/Bottom/mefButton").getComponent(cc.Button);

        this.mefButton.node.on('click', function () {
            self.mainPageView.getComponent(cc.PageView).scrollToPage(3);
        });
        self.mainPageView.on('page-turning', function () {
            switch (self.mainPageView.getComponent(cc.PageView).getCurrentPageIndex()) {
                case 0:
                    self.storeButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("choosestore");
                    self.giftButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("gift");
                    self.shelfButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("shelf");
                    self.mefButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("me");

                    break;
                case 1:
                    self.storeButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("store");
                    self.giftButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("choosegift");
                    self.shelfButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("shelf");
                    self.mefButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("me");
                    break;
                case 2:
                    self.storeButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("store");
                    self.giftButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("gift");
                    self.shelfButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("chouuseshef");
                    self.mefButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("me");
                    break;
                case 3:
                    self.storeButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("store");
                    self.giftButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("gift");
                    self.shelfButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("shelf");
                    self.mefButton.node.getComponent(cc.Sprite).spriteFrame = self.puzzleAtlas1.getSpriteFrame("chooseme");
                    break;


            }
        });
    },

    start() {

    },
    playPuzzle() {
        //cc.director.loadScene("puzzleMain");
        cc.find("Canvas/ShelfPuzzleInfo").active=true;
    },
    update(dt) {
        this.timer -= dt;
        if (this.timer <= 0) {
            this.storeSmallPageView.getComponent(cc.PageView).scrollToPage((this.storeSmallPageView.getComponent(cc.PageView).getCurrentPageIndex() + 1) % 2);
            this.meSmallPageView.getComponent(cc.PageView).scrollToPage((this.meSmallPageView.getComponent(cc.PageView).getCurrentPageIndex() + 1) % 2);
            this.timer = 5;
        }
    },
});
